define(['require', 'jquery', 'underscore', './util','./CommonTools'], function (require, $, _, util, dialog) {
    require('../../less/dateselect.less');
    var DatePicker = function (opt) {
        var defaultOpt = {
            title: ['日', '一', '二', '三', '四', '五', '六'],
            time: new Date,
            container:'.date-picker-container',
            next:'.icon-next',
            prev:'.icon-prev',
            titleMonth:'.month',
            onNextCallback: null,
            onPrevCallback:null,
            onCellClick:null,
            showHasFlag:null,
            maxDate:new Date,
            timeFormat:'/'
        };
        this.opt = $.extend(defaultOpt, opt);
        this._init();

    };
    DatePicker.prototype._init = function () {
        var _this = this;
        console.log('init');
        var html = this.getHtml(this.opt.time);
        $(this.opt.next).on('click', $.proxy(this.goNext, this));
        $(this.opt.prev).on('click', $.proxy(this.goPrev, this));
        this.$container = $(this.opt.container);
        $(this.opt.container).html('<div class="date-picker-swiper">' + html + '</div>');
        this.$wrapper = this.$container.find('.date-picker-swiper');
        $(this.opt.titleMonth).text(this.current.getFullYear() + '年'+ (this.current.getMonth()+1) +'月');
        this.clickNum = 0;
        this.$container.on('click', '.date-picker-cell', function (e) {
            _this.$container.find('.date-picker-cell').removeClass('active');
            if((typeof _this.opt.onCellClick) === 'function') {
                _this.opt.onCellClick(e);
            }

        });
        if(typeof this.opt.showHasFlag === 'function') {
            this.opt.showHasFlag(this.$container, [this.current] );
        }
    }
    //计算每月的时间行列
    DatePicker.prototype.getArray = function (date) {
        if(!date) date = this.opt.time;
        if(typeof date === 'string') date = new Date(date);
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var MonthFirst = new Date(year, month-1, 1);
        var Day = MonthFirst.getDay();
        var isFirst = (Day === 0);
        var start = isFirst ? util.formatDate(MonthFirst) : util.formatDate(MonthFirst, '-' + Day + 'd');
        var col = 7;
        var row = 6;
        var map = [];
        for (var i = 0; i < row; i++) {
            var array = [];
            for (var j = 0; j < col; j++) {
                var index = i * 7 + (j);
                array.push(util.formatDate(start, '+' + index + 'd'));
            }
            map.push(array);
        }
        return map;
    }
    //渲染每个月的html
    DatePicker.prototype.render = function (date, flag) {
        var _this = this;
        if(!date) date = new Date();
        if(typeof date === 'string') date = new Date(date);
        var map = this.getArray(date);
        var month = date.getMonth() + 1;
        var year = date.getFullYear();
        var tmp = '';
        var title = `<div class="date-picker-row date-picker-title">`;
        this.opt.title.forEach(function (item, index) {
            title += `<div class="date-picker-cell">${item}</div>`;
        });
        title += '</div>';
        tmp += `<div class="date-picker-month date-picker-${flag}" data-month="${year}/${month}">`;
        tmp += title;
        map.forEach(function (item, index) {
            tmp += `<div class="date-picker-row">`;
            item.forEach(function (v, i) {
                var className = (new Date(v).getMonth() + 1) === month ? 'date-picker-enable': 'date-pikcer-disabled';
                tmp += `<div class="no-flag date-picker-cell ${className}" data-date="${v.replace(/\//g, _this.opt.timeFormat)}"><span>${new Date(v).getDate()}</span></div>`;
            });
            tmp += `</div>`;

        });
        return tmp + '</div>';
    }
    //渲染三个月的html
    DatePicker.prototype.getHtml = function (date) {
        if(typeof date === 'string') date = new Date(date);
        var month = date.getMonth();
        var year = date.getFullYear();
        var prevMonth = month ?  year + '/' + month  + '/01' : (year - 1) +'/' + 12 +'/01';
        var nextMonth = month === 11 ?  (year + 1)  +'/01/01' : year + '/' + (month + 2) + '/01';
        this.current = date;
        this.prev = new Date(prevMonth);
        this.next = new Date(nextMonth);
        var preHtml = this.render(prevMonth, 'prev');
        var currentHtml = this.render(date, 'current');
        var nextHtml = this.render(nextMonth, 'next');
        return preHtml + currentHtml + nextHtml;
    }
    DatePicker.prototype.getDatePrev = function (date, isPrev) {
        if(typeof date === 'string') date = new Date(date);
        var month = date.getMonth();
        var year = date.getFullYear();
        var prevMonth = month ?  year + '/' + month  + '/01' : (year - 1) +'/' + 12 +'/01';
        var nextMonth = month === 11 ?  (year + 1)  +'/01/01' : year + '/' + (month + 2) + '/01';
        if(isPrev) {
            return new Date(prevMonth);
        } else {
            return new Date(nextMonth);
        }

    }
    DatePicker.prototype.goNext = function () {
        var _this = this;
        var currentYear = _this.current.getFullYear();
        var currentMonth = _this.current.getMonth();
        var MaxYear =  _this.opt.maxDate.getFullYear();
        var MaxMonth = _this.opt.maxDate.getMonth();
        console.log(MaxMonth,MaxYear, currentMonth, currentYear);
        if(currentYear > MaxYear || (MaxYear === currentYear && currentMonth >= MaxMonth)) {
            return dialog.Toast('超过最大日期');
        }
        _this.prev = _this.current;
        _this.current = _this.next;
        _this.next = _this.getDatePrev(_this.current, false);
        _this.clickNum ++;
        var newHtml = _this.render(_this.next, 'next');
        $(_this.opt.container).find('.date-picker-swiper').css({
            transform:'translate3d('+ -(_this.clickNum * 100)  +'%,0, 0)'
        });
        _this.$wrapper.find('.date-picker-prev').remove().end().append(newHtml);
        _this.$wrapper.find('.date-picker-month').each(function (index, item) {
            var t ;
            if(!index) {
                t = _this.clickNum - 1;
                $(item).removeClass('date-picker-current').addClass('date-picker-prev');
            } else if(index === 1) {
                t = _this.clickNum;
                $(item).removeClass('date-picker-next').addClass('date-picker-current');
            } else {
                t = _this.clickNum + 1;
            }
            $(item).css({
                transform:'translate3d('+ t * 100  +'%,0, 0)'
            });
        });
        $(this.opt.titleMonth).text(this.current.getFullYear() + '年'+ (this.current.getMonth()+1) +'月');
        if(typeof this.opt.onPrevCallback === 'function') this.opt.onPrevCallback(this.current);
        if(typeof this.opt.showHasFlag === 'function') {
            this.opt.showHasFlag(this.$container, [this.current] );
        }
    }

    //向前进一个月
    DatePicker.prototype.goPrev = function () {
        var _this = this;
        _this.next = _this.current;
        _this.current = _this.prev;
        _this.prev = _this.getDatePrev(_this.current, true);
        _this.clickNum--;
        var newHtml = _this.render(_this.prev, 'prev');
        $(_this.opt.container).find('.date-picker-swiper').css({
            transform:'translate3d('+ -(_this.clickNum * 100)  +'%,0, 0)'
        });
        _this.$wrapper.find('.date-picker-next').remove().end().prepend(newHtml);

        _this.$wrapper.find('.date-picker-month').each(function (index, item) {
            var t;
            if(!index) {
                t = _this.clickNum - 1;

            } else if(index === 1) {
                t = _this.clickNum;
                $(item).removeClass('date-picker-prev').addClass('date-picker-current');

            } else {
                t = _this.clickNum + 1;
                $(item).removeClass('date-picker-current').addClass('date-picker-next');
            }
            $(item).css({
                transform:'translate3d('+ t * 100  +'%,0, 0)'
            });
        });
        $(this.opt.titleMonth).text(this.current.getFullYear() + '年'+ (this.current.getMonth()+1) +'月');
        if(typeof this.opt.onPrevCallback === 'function') this.opt.onPrevCallback(this.current);
        if(typeof this.opt.showHasFlag === 'function') {
            this.opt.showHasFlag(this.$container, [this.current] );
        }

    }
    return DatePicker;
});
